﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace yaai
{
    public class WordList
    {
        private String name;
        private List<String> words;
        private String fileName;
        public bool changed;

        public WordList(String name, List<String> list, String fileName)
        {
            this.fileName = fileName;
            this.name = name;
            words = list;
            words.Sort();
            changed = false;
        }

        public WordList(String name, List<String> list)
        {
            this.fileName = name;
            this.name = name;
            changed = false;
            words = list;
            words.Sort();
        }

        public WordList(String name)
        {
            this.name = name;
            this.fileName = name;
            this.changed = false;
            words = new List<string>();
        }

        public WordList(String name, String fileName)
        {
            this.name = name;
            this.fileName = fileName;
            this.changed = false;
            words = new List<string>();
        }

        public String getFileName()
        {
            return fileName;
        }

        public void setFileName(String name)
        {
            this.name = name;
        }


        public List<String> getWordList()
        {
            return words;
        }

        public void removeWord(String word)
        {
            changed = true;
            words.Remove(word);
        }

        public String getRandomWord()
        {
            Random r = new Random();
            return ElementAt(r.Next(getWordCount()));
        }

        public String ElementAt(int i)
        {
            return words.ElementAt(i);
        }

        public void addWord(String word)
        {
            words.Add(word);
            words.Sort();
            changed = true;
        }

        public String getName()
        {
            return name;
        }

        public void setName(String name)
        {
            this.name = name;
        }

        public int getWordCount()
        {
            return words.Count;
        }

        public void writeAllToFile()
        {
            if(changed)
            {
                using (System.IO.StreamWriter file = new System.IO.StreamWriter("../../lists/" + getFileName() + ".txt"))
                {
                    foreach (string line in words)
                    {
                        file.WriteLine(line);
                    }
                }
            }
            changed = false;
        }

        public void readFromFile()
        {
            using (StreamReader sr = new StreamReader(getFileName()))
            {
                String line;
                while ((line = sr.ReadLine()) != null)
                {
                    if (line == "" || line == " " || line == "\n")
                        continue;
                    addWord(line.Trim());
                }
            }
        }
    }
}
